﻿using System;
using System.Text;

namespace Stauffware.Common.Core
{
	/// <summary>
	/// Extension methods for DateTime and TimeSpan.
	/// </summary>
	public static class Time_Extensions
	{
		#region TimeSpan extensions

		// TimeSpan is, by definition, equivalent to an Int64 containing the number
		// of ticks.  1 tick = 100 nanoseconds, or 1/10,000,000 second. 

		//-----------------------------------------------------------------------------		
		/// <summary>
		/// Multiplies the TimeSpan by an integer amount.
		/// </summary>

		public static TimeSpan Multiply (this TimeSpan ts, int n)
		{
			return TimeSpan.FromTicks(ts.Ticks * n);
		}

		//-----------------------------------------------------------------------------		
		/// <summary>
		/// Divides the TimeSpan by an integer amount.
		/// </summary>

		public static TimeSpan Divide (this TimeSpan ts, int n)
		{
			return TimeSpan.FromTicks(ts.Ticks / n);
		}

		//-----------------------------------------------------------------------------		
		/// <summary>
		/// Multiplies the TimeSpan by a double amount.
		/// </summary>

		public static TimeSpan Multiply (this TimeSpan ts, double n)
		{
			return TimeSpan.FromTicks((long) Math.Round(n * (double) ts.Ticks));
		}

		//-----------------------------------------------------------------------------		
		/// <summary>
		/// Divides the TimeSpan by a double amount.
		/// </summary>

		public static TimeSpan Divide (this TimeSpan ts, double n)
		{
			return TimeSpan.FromTicks((long) Math.Round(((double) ts.Ticks) / n));
		}

		//-----------------------------------------------------------------------------
		/// <summary>
		/// Returns true if the TimeSpan is less than TimeSpan.Zero, false otherwise.
		/// </summary>

		public static bool IsNegative (this TimeSpan ts)
		{
			return ts < TimeSpan.Zero;
		}

		//-----------------------------------------------------------------------------

		#endregion
	}
}
